package com.jk.controller;

import com.jk.entity.New;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by dell on 2017/6/9.
 */
@RequestMapping("/ajax")
@Controller
public class AjaxController {

//    @RequestMapping(value = "/test", produces = "application/json;charset=utf-8")
//    public
//    @ResponseBody
//    New test(@RequestBody New n, HttpServletResponse response) {
//        response.addHeader("Access-Control-Allow-Origin", "*");
//        New n2 = new New();
//        n2.setContext("My name is n2.");
//        n2.setId(2);
//        n2.setTitle("n2");
//        return n2;
//    }

    /**
     * 联合以下代码，实现ajax跨域请求
     * @param n
     * @param response
     * @return
     */
    @RequestMapping("/test")
    public @ResponseBody New test(New n, HttpServletResponse response){
        response.addHeader("Access-Control-Allow-Origin", "*");
        System.out.println("id:"+n.getId());
        System.out.println("title:"+n.getTitle());
        System.out.println("context:"+n.getContext());
        New n2 = new New();
        n2.setId(2);
        n2.setTitle("n2");
        n2.setContext("My name is N2.");
        return n2;
    }
}
//    function test4(){
//        $.ajax({
//                url:'http://localhost:8080/ajax/test.do',
//                type:'post',
//                data:{id:'1',title:'n1',context:'My name is N1.'},
//        success:function(s){
//            for(var i in s){
//                alert(s[i]);
//            }
//        },dataType:'json'
//        })
//    }